package com.webapp.controller;

import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Employee;
import com.webapp.model.Manager;
import com.webapp.model.ManagerModel;



/**
 * Servlet implementation class CreateAd
 */
public class CreateEmployee extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateEmployee() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		int employeeSSN = Integer.parseInt((String)request.getParameter("ssnTxt"));
		String employeeLastName = request.getParameter("lastTxt");
		String employeeFirstName = request.getParameter("firstTxt");
		String employeeAddress = request.getParameter("addTxt");
		String employeeCity = request.getParameter("cityTxt");
		String employeeState = request.getParameter("stateTxt");
		int employeeZip = Integer.parseInt((String)request.getParameter("zipTxt"));
		int employeePhone = Integer.parseInt((String)request.getParameter("phoneTxt"));
		int employeeRate = Integer.parseInt(request.getParameter("rateTxt"));
		String employeeEmail = request.getParameter("emailTxt");
		String employeePassword = request.getParameter("passTxt");
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		String date = dateFormat.format(cal.getTime());
		Employee em = new Employee(employeeSSN);
		ManagerModel mm = new ManagerModel();
		em.setLastName(employeeLastName);
		em.setFirstName(employeeFirstName);
		em.setAddress(employeeAddress);
		em.setCity(employeeCity);
		em.setState(employeeState);
		if(session.getAttribute("edit") == null)
			em.setStartDate(date);
		em.setZip(employeeZip);
		em.setRate(employeeRate);
		em.setPhone(employeePhone);
		em.setManager(((Manager)session.getAttribute("UserObject")).getSSN());
		em.setEmail(employeeEmail);
		em.setPassword(employeePassword);
		em.setEmployeeId((int)Math.random()*100000000);
		boolean flag = false;
		boolean flag2 = false;
		if(session.getAttribute("edit") == null)
		{
			flag = mm.addEmployee(em);
		}
		else
		{
			flag2 = mm.editEmployee(em);
		}
		if(flag) 
		{
			List newList = mm.getAllEmployees();
			session.setAttribute("Employees", newList);
			response.sendRedirect("ManageEmployees.jsp?employee=added");
		}
		else if(flag2 == false)
		{
			List newList = mm.getAllEmployees();
			session.setAttribute("Employees", newList);
			response.sendRedirect("ManageEmployees.jsp?employee=edited");
		}
		else {
			response.sendRedirect("Error.jsp");
		}
	}
}
